﻿function TextEditorViewModel(model) {
    var selfModel = this;
    ko.mapping.fromJS(model, {}, selfModel);

    selfModel.EditorObject = ko.observable();
    selfModel.EditorContainer = ko.observable();
    selfModel.ResultContainer = ko.observable();

    selfModel.Init = function (editorId, resultId) {
        selfModel.EditorContainer(editorId);
        selfModel.ResultContainer(resultId);

        CKEDITOR.replace(selfModel.EditorContainer());
        selfModel.EditorObject(CKEDITOR.instances[selfModel.EditorContainer()]);
    };

    selfModel.Save = function (target) {
        var obtainedData = selfModel.EditorObject().getData();

        $("#" + selfModel.ResultContainer()).html(obtainedData);

        if (target && obtainedData)
            target(obtainedData);
    };
}